package com.fss.home.service.impl;

import com.fss.home.domain.*;
import com.fss.home.mapper.HomeMapper;
import com.fss.home.service.HomeService;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
public class HomeServiceImpl implements HomeService
{
    @Autowired
    private HomeMapper homeMapper;
    @Override
    public List<Home> getSwiperImages(){
        return homeMapper.getSwiperImages();
    };
    @Override
    public List<Home> getGoodList(){
        return homeMapper.getGoodList();
    };
    @Override
    public List<Product> getProductWithImages() {
        return homeMapper.selectProductWithImages();
    }
    @Override
    public  List<Category2>getAllCategories(){
        return homeMapper.getAllCategories();
    }
    @Override
    public List<Product> selectCateToProduct(long cateGoryId, Double priceMin, Double priceMax, int sortType) {
        System.out.println("Category ID: " + cateGoryId + ", Sort Type: " + sortType + ", Price Min: " + priceMin + ", Price Max: " + priceMax);
        return homeMapper.selectCateToProduct(cateGoryId, priceMin, priceMax, sortType);
    }
    @Override
    public Product getProductDetail(Long productId) {
        return homeMapper.selectProductDetail(productId);
    }
    @Override
    public List<ProductSku> getProductSkuByProductId(Long productId) {
        return homeMapper.selectProductSkuByProductId(productId);
    }
    @Override
   public List<Delivery> getDeliveryInfoByUserId(Long userId) {
        return homeMapper.getDeliveryInfoByUserId(userId);
    }
     @Override
    public Long addDeliveryInfo(Delivery delivery) {
        homeMapper.addDeliveryInfo(delivery);
        return delivery.getDeliveryId();
    }

    @Override
    public boolean updateDeliveryInfo(Delivery delivery) {
        return homeMapper.updateDeliveryInfo(delivery) > 0;
    }
    @Override
public boolean deleteDeliveryInfo(Long deliveryId) {
    return homeMapper.deleteDeliveryInfo(deliveryId) > 0;
}
    @Override
    @Transactional
    public void addProduct(Product productEntity) {
        // 1. 插入商品主体
        homeMapper.insertProduct(productEntity);

        // 2. 插入商品图片
        if (productEntity.getImages() != null && !productEntity.getImages().isEmpty()) {
            for (ProductImage image : productEntity.getImages()) {
                image.setProductId(productEntity.getProductId()); // 关联商品ID
                homeMapper.insertProductImage(image);
            }
        }

        // 3. 插入商品SKU
        if (productEntity.getSkus() != null && !productEntity.getSkus().isEmpty()) {
            for (ProductSku sku : productEntity.getSkus()) {
                sku.setProductId(productEntity.getProductId()); // 绑定商品ID
                homeMapper.insertProductSku(sku);
            }
        }
    }
    @Override
    public List<Product> searchProductsByName(String productName) {
        return homeMapper.selectProductByName(productName);
    }

    @Override
    public void deleteProduct(Long productId) {
        // 删除商品的图片
        homeMapper.deleteProductImages(productId);

        // 删除商品的SKU
        homeMapper.deleteProductSkus(productId);

        // 删除商品本身
        homeMapper.deleteProduct(productId);
    }
    //修改商品
    @Override
    public void updateProduct(Product product) {
        // 更新商品主信息
        homeMapper.updateProduct(product);

        // 插入新的商品图片，并关联 productId
        if (product.getImages() != null && !product.getImages().isEmpty()) {
            for (ProductImage image : product.getImages()) {
                image.setProductId(product.getProductId()); // 关联 productId
                homeMapper.insertProductImage(image);
            }
        }

        // 删除旧的商品 SKU
        homeMapper.deleteProductSkus(product.getProductId());

        // 插入新的商品 SKU，并关联 productId
        if (product.getSkus() != null && !product.getSkus().isEmpty()) {
            for (ProductSku sku : product.getSkus()) {
                sku.setProductId(product.getProductId()); // 关联 productId
                homeMapper.insertProductSku(sku);
            }
        }
    };
    @Override
    public void deleteImage(Long imageId) {
         homeMapper.deleteImagesById(imageId) ;
    }
}
